package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.domain.entity.ProjectType;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.service.IProjectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检验项目类型Controller
 *
 * @author cell
 * @date 2023-09-12
 */
@Api(tags = "检验项目类型")
@RestController
@RequestMapping("/masterData/projectType")
public class ProjectTypeController extends BaseController
{
    @Autowired
    private IProjectTypeService projectTypeService;

    /**
     * 查询检验项目类型列表
     */
    @ApiOperation("查询检验项目类型列表")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:list')")
    @GetMapping("/list")
    public AjaxResult list(ProjectType projectType)
    {
        List<ProjectType> list = projectTypeService.selectProjectTypeList(projectType);
        return success(list);
    }

    /**
     * 导出检验项目类型列表
     */
    @ApiOperation("导出检验项目类型列表")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectType projectType)
    {
        List<ProjectType> list = projectTypeService.selectProjectTypeList(projectType);
        ExcelUtil<ProjectType> util = new ExcelUtil<ProjectType>(ProjectType.class);
        util.exportExcel(response, list, "检验项目类型数据");
    }

    /**
     * 获取检验项目类型详细信息
     */
    @ApiOperation("获取检验项目类型详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectTypeService.selectProjectTypeById(id));
    }

    /**
     * 新增检验项目类型
     */
    @ApiOperation("新增检验项目类型")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ProjectType projectType)
    {
        if (!projectTypeService.checkProjectTypeNameUnique(projectType))
        {
            return error("新增检验项目类型'" + projectType.getProjectTypeName() + "'失败，名称已存在");
        }
        projectType.setCreateBy(getUsername());
        return toAjax(projectTypeService.insertProjectType(projectType));
    }

    /**
     * 修改检验项目类型
     */
    @ApiOperation("修改检验项目类型")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectType projectType)
    {
        Long typeId = projectType.getId();
        if (!projectTypeService.checkProjectTypeNameUnique(projectType))
        {
            return error("修改检验项目类型'" + projectType.getProjectTypeName() + "'失败，名称已存在");
        }
        else if (projectType.getParentId().equals(typeId))
        {
            return error("修改检验项目类型'" + projectType.getProjectTypeName()+ "'失败，上级类型不能是自己");
        }
        projectType.setUpdateBy(getUsername());
        return toAjax(projectTypeService.updateProjectType(projectType));
    }

    /**
     * 删除检验项目类型
     */
    @ApiOperation("删除检验项目类型")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:remove')")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if (projectTypeService.hasChildById(id))
        {
            return warn("存在下级,不允许删除");
        }
        // 检查是否存在检验项目
        if (projectTypeService.checkProjectTypeExistProject(id))
        {
            return warn("存在检验项目,不允许删除");
        }
        return toAjax(projectTypeService.deleteProjectTypeById(id));
    }

    /**
     * 获取检验项目类型树列表
     */
    @ApiOperation("获取检验项目类型树列表")
    @PreAuthorize("@ss.hasPermi('masterData:projectType:list')")
    @GetMapping("/materialTypeTree")
    public AjaxResult deptTree(ProjectType projectType)
    {
        return success(projectTypeService.selectProjectTypeTreeList(projectType));
    }
}
